package com.xoa.dev.pz.service.impl;

import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.party.GridInfoMapper;
import com.xoa.dev.pz.dao.MenuJdtMapper;
import com.xoa.dev.pz.dao.PzPersonMapper;
import com.xoa.dev.pz.dao.PzKeyYouthMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.party.GridInfo;
import com.xoa.dev.pz.model.PzPersonWithBLOBs;
import com.xoa.dev.pz.model.PzKeyYouth;
import com.xoa.dev.pz.model.PzKeyYouthWithBLOBs;
import com.xoa.model.users.Users;
import com.xoa.service.sys.SysLogService;
import com.xoa.service.users.UsersService;
import com.xoa.util.DateFormat;
import com.xoa.util.ExcelUtil;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.L;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.netdisk.CheckAll;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class PzKeyYouthService {
    @Resource
    private PzKeyYouthMapper pzKeyYouthMapper;
    @Resource
    private UsersService usersService;
    @Resource
    private PzPersonMapper pzPersonMapper;
    @Resource
    private SysLogService sysLogService;
    @Resource
    private SysCodeMapper sysCodeMapper;
    @Resource
    GridInfoMapper gridInfoMapper;
    @Resource
    private MenuJdtMapper menuJdtMapper;
    /**
     * 插入
     * @param pzKeyYouthWithBLOBs
     * @return
     */
    @Transactional
    public ToJson insertSelective(HttpServletRequest request, PzKeyYouthWithBLOBs pzKeyYouthWithBLOBs){
        ToJson toJson=new ToJson(1,"err");
        try {
            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
            pzKeyYouthWithBLOBs.setCreateUser(user.getUserId());
            pzKeyYouthWithBLOBs.setCreateTime(DateFormat.getDatestr(new Date()));
            int temp=pzKeyYouthMapper.insertSelective(pzKeyYouthWithBLOBs);
            sysLogService.setPzLog(request,user.getUserId(),new Date(),"1","5","重点青少年");
            if(temp>0){
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }
        }catch (Exception e) {
            toJson.setMsg(e.getMessage());
            L.e(e);
        }
        return toJson;
    }

    /**
     * 主键删除
     * @param keyYouthId
     * @return
     */
    @Transactional
    public ToJson deleteByPrimaryKey(HttpServletRequest request, int keyYouthId){
        ToJson toJson=new ToJson(1,"err");
        try {
            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
            int temp=pzKeyYouthMapper.deleteByPrimaryKey(keyYouthId);
            sysLogService.setPzLog(request,user.getUserId(),new Date(),"2","5","重点青少年");
            if(temp>0){
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }
        }catch (Exception e) {
            toJson.setMsg(e.getMessage());
            L.e(e);
        }
        return toJson;
    }

    /**
     * 更新
     * @param pzKeyYouthWithBLOBs
     * @return
     */
    @Transactional
    public ToJson upServiceById(HttpServletRequest request, PzKeyYouthWithBLOBs pzKeyYouthWithBLOBs){
        ToJson toJson=new ToJson(1,"err");
        try {
            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
            int temp=pzKeyYouthMapper.updateByPrimaryKeySelective(pzKeyYouthWithBLOBs);
            sysLogService.setPzLog(request,user.getUserId(),new Date(),"3","5","重点青少年");
            if(temp>0){
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }
        }catch (Exception e) {
            toJson.setMsg(e.getMessage());
            L.e(e);
        }
        return toJson;


    }

    /**
     * 分页查询
     * @param pzKeyYouthWithBLOBs
     * @param page
     * @param pageSize
     * @param useFlag
     * @return
     */
    @Transactional
    public ToJson<PzKeyYouthWithBLOBs> selectService(HttpServletResponse response, HttpServletRequest request, PzKeyYouthWithBLOBs pzKeyYouthWithBLOBs,
                                                     Integer page, Integer pageSize, boolean useFlag, String export, String pCradId, String pName){
        ToJson<PzKeyYouthWithBLOBs> toJson=new ToJson<>(1,"err");
        try {
            //分页
            PageParams pageParams = new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(pageSize);
            pageParams.setUseFlag(useFlag);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("page", pageParams);
            map.put("pCradId", pCradId);
            map.put("pName", pName);
            Map<String, Object> param = new HashMap<String, Object>();
            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
            param.put("userId", user.getUserId());
            param.put("userPriv", user.getUserPriv());
            param.put("deptId", user.getDeptId());
            String menuModel=menuJdtMapper.selMenuByModel("4");
            if(!CheckAll.checkAll(menuModel,param)){
                pzKeyYouthWithBLOBs.setCreateUser(user.getUserId());
            }
            map.put("ps",pzKeyYouthWithBLOBs);
            if(export==null){
                export="0";
            }
            List<PzKeyYouthWithBLOBs> temp=pzKeyYouthMapper.selectService(map);
            for(PzKeyYouthWithBLOBs ps:temp){
                ps.setGridUserNames(usersService.getUserNameById(ps.getGridUserIds()));
                SysCode sysCode = sysCodeMapper.getSingleCode("RENT_CONCERN", ps.getAttention());
                if(sysCode!=null){
                    ps.setAttentionStr(sysCode.getCodeName());
                }
                SysCode sysCode1 = sysCodeMapper.getSingleCode("MANAGEMENT_TOOL", ps.getManagementTool());
                if(sysCode1!=null){
                    ps.setManagementToolStr(sysCode1.getCodeName());
                }
                SysCode sysCode2 = sysCodeMapper.getSingleCode("LLLEGAL_SITUATION", ps.getLllegalSituation());
                if(sysCode2!=null){
                    ps.setLllegalSituationStr(sysCode2.getCodeName());
                }
                SysCode sysCode3 = sysCodeMapper.getSingleCode("P_CRIME_TYPE", ps.getCriminalType());
                if(sysCode3!=null){
                    ps.setCriminalTypeStr(sysCode3.getCodeName());
                }
                SysCode sysCode4 = sysCodeMapper.getSingleCode("SCHOOL_HIERARCHY", ps.getSchoolHierarchy());
                if(sysCode4!=null){
                    ps.setSchoolHierarchyStr(sysCode4.getCodeName());
                }
                SysCode sysCode5 = sysCodeMapper.getSingleCode("PERSON_TYLE", ps.getPersonTyle());
                if(sysCode5!=null){
                    ps.setPersonTyleStr(sysCode5.getCodeName());
                }

                SysCode sysCode6 = sysCodeMapper.getSingleCode("POLITICAL", ps.getpPolitical());
                if(sysCode6!=null){
                    ps.setpPoliticalStr(sysCode6.getCodeName());
                }
                SysCode sysCode7 = sysCodeMapper.getSingleCode("DEGREE", ps.getpEducation());
                if(sysCode7!=null){
                    ps.setpEducationStr(sysCode7.getCodeName());
                }

                if ("0".equals(ps.getpSex())) {
                    ps.setpSexStr("男");
                } else {
                    ps.setpSexStr("女");
                }
                if ("01".equals(ps.getpMaritalStatus())) {
                    ps.setpMaritalStatusStr("未婚");
                } else {
                    ps.setpMaritalStatusStr("已婚");
                }
                if ("0".equals(ps.getpVehicles())) {
                    ps.setpVehiclesStr("无");
                } else {
                    ps.setpVehiclesStr("有");
                }

            }
            toJson.setObj(temp);
            toJson.setFlag(0);
            toJson.setMsg("ok");
            toJson.setTotleNum(pageParams.getTotal());
            if(export.equals("1")){
                HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("重点青少年基本信息导出", 34);
                String[] secondTitles = {"所属网格", "姓名","性别","身份证", "曾用名/别名", "出生日期","民族","联系方式","政治面貌","文化程度","婚姻状况","宗教信仰","职业","工作单位","户籍地址","籍贯","户籍地详址","现居地址","名下机动车辆","备注","关注程度","管理手段","违法情况","犯罪类型","学校所属层级","教育学校名称","人员类型"};
                HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);
                String[] beanProperty = { "gridName","pName","pSexStr","pCradId", "pAlias","pDate","pNation","pPhone","pPoliticalStr","pEducationStr","pMaritalStatusStr","pReligion","pOccupation","pWorkUnit","pDomicile","pOrigin","pDetailedDomicile","pAddress","pVehiclesStr","pRemarks",  "attentionStr","managementToolStr","lllegalSituationStr","criminalTypeStr","schoolHierarchyStr","schoolName","personTyleStr"};
                HSSFWorkbook workbook = null;
                workbook = ExcelUtil.exportExcelData(workbook2, temp, beanProperty);
                OutputStream out = response.getOutputStream();
                String filename = "重点青少年基本信息记录表.xls";
                filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
                response.setContentType("application/vnd.ms-excel;charset=UTF-8");
                response.setHeader("content-disposition", "attachment;filename=" + filename);
                workbook.write(out);
                out.close();
            }
        }catch (Exception e) {
            toJson.setMsg(e.getMessage());
            L.e(e);
        }
        return toJson;



    }

    /**
     * 主键查询
     * @param keyYouthId
     * @return
     */
    @Transactional
    public ToJson<PzKeyYouthWithBLOBs> selectById(int keyYouthId, HttpServletRequest request){
        ToJson toJson=new ToJson(1,"err");
        try {
            PzKeyYouthWithBLOBs temp=pzKeyYouthMapper.selectByPrimaryKey(keyYouthId);
            //附件处理
            List<Attachment> attachmentList=new ArrayList<Attachment>();
            if(temp.getAttachmentName()!=null&&!"".equals(temp.getAttachmentName())){
                attachmentList= GetAttachmentListUtil.returnAttachment(temp.getAttachmentName(),temp.getAttachmentId(),"xoa" + (String) request.getSession().getAttribute("loginDateSouse"), GetAttachmentListUtil.MODULE_PZ_YOUTH);
            }
            temp.setAttachmentList(attachmentList);
            if(temp!=null){
                toJson.setMsg("ok");
                toJson.setFlag(0);
                toJson.setObject(temp);
            }
        }catch (Exception e) {
            toJson.setMsg(e.getMessage());
            L.e(e);
        }
        return toJson;
    }




    @Transactional
    public ToJson importPzKeyYouthAcess(HttpServletRequest request, HttpServletResponse response, MultipartFile file, HttpSession session) {
        ToJson<PzKeyYouthWithBLOBs> toJson=new ToJson<>(1,"err");
        Users users= SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users());
        // 成功次数
        Integer successCount = 0;
        try {
            //判读当前系统
            //读取配置文件
            ResourceBundle rb = ResourceBundle.getBundle("upload");
            String osName = System.getProperty("os.name");
            StringBuffer path = new StringBuffer();
            if (osName.toLowerCase().startsWith("win")) {
                path = path.append(rb.getString("upload.win"));
            } else {
                path = path.append(rb.getString("upload.linux"));
            }
            // 判断是否为空文件
            if (file.isEmpty()) {
                toJson.setMsg("请上传文件！");
                toJson.setFlag(1);
                return toJson;
            } else {
                String fileName = file.getOriginalFilename();
                if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {
                    String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                    int pos = fileName.indexOf(".");
                    String extName = fileName.substring(pos);
                    String newFileName = uuid + extName;
                    File dest = new File(path.toString(), newFileName);
                    file.transferTo(dest);
                    // 将文件上传成功后进行读取文件
                    // 进行读取的路径
                 //   String readPath = path.append(newFileName).toString();
                    String readPath = path.append(System.getProperty("file.separator")).append(newFileName).toString();
                    InputStream in = new FileInputStream(readPath);
                    HSSFWorkbook excelObj = new HSSFWorkbook(in);
                    HSSFSheet sheetObj = excelObj.getSheetAt(0);
                    Row row = sheetObj.getRow(0);
                    int colNum = row.getPhysicalNumberOfCells();
                    int lastRowNum = sheetObj.getLastRowNum();
                    List<PzKeyYouthWithBLOBs> saveList = new ArrayList<PzKeyYouthWithBLOBs>();
                    List<PzPersonWithBLOBs> saveList1 = new ArrayList<PzPersonWithBLOBs>();
                    PzKeyYouthWithBLOBs pzKeyYouthWithBLOBs = null;
                    SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
                    PzPersonWithBLOBs pzPersonWithBLOBs = null;
                    int count = 0;
                    for (int i = 1; i <= lastRowNum; i++) {
                        pzKeyYouthWithBLOBs = new PzKeyYouthWithBLOBs();
                        pzPersonWithBLOBs = new PzPersonWithBLOBs();
                        row = sheetObj.getRow(i);
                        String value = "";
                        if (row != null) {
                            for (int j = 0; j < colNum; j++) {
                                Cell cell = row.getCell(j);
                                if (cell != null) {
                                    switch (j) {
                                        case 0:
                                            //所属网格
                                            String as = cell.getStringCellValue();
                                            GridInfo info = gridInfoMapper.selectByUGridName(as);
                                            pzPersonWithBLOBs.setpGrid(info.getGridId());
                                            pzKeyYouthWithBLOBs.setGridId(info.getGridId());
                                            break;
                                        case 1:
                                            // 姓名
                                            pzPersonWithBLOBs.setpName(cell.getStringCellValue());
                                            break;
                                        case 2:
                                            // 身份证
                                            pzPersonWithBLOBs.setpCradId(cell.getStringCellValue());
                                            break;
                                        case 3:
                                            // 别名
                                            pzPersonWithBLOBs.setpAlias(cell.getStringCellValue());
                                            break;
                                        case 4:
                                            // 性别
                                            if (cell.getStringCellValue().equals("女") ) {
                                                pzPersonWithBLOBs.setpSex("1");
                                            } else if (cell.getStringCellValue().equals("男")) {
                                                pzPersonWithBLOBs.setpSex("0");
                                            }
                                            break;
                                        case 5:
                                            // 出生日期
                                            if(cell.getCellType() == CellType.NUMERIC){
                                                short format = cell.getCellStyle().getDataFormat();
                                                SimpleDateFormat sdf = null;
                                                if(format == 14 || format == 31 || format == 57 || format == 58){
                                                    //日期
                                                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                                                }else if (format == 20 || format == 32) {
                                                    //时间
                                                    sdf = new SimpleDateFormat("HH:mm");
                                                }
                                                double v = cell.getNumericCellValue();
                                                Date dates = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(v);
//                                                date = sdf.format(v);
                                                pzKeyYouthWithBLOBs.setpDate(dates);
                                                break;
                                            }
                                            pzPersonWithBLOBs.setpDate(sd.parse(cell.getStringCellValue()));
                                            break;
                                        case 6:
                                            // 民族
                                            pzPersonWithBLOBs.setpNation(cell.getStringCellValue());
                                            break;
                                        case 7:
                                            // 联系方式
                                            if (cell.getCellType() ==CellType.NUMERIC) {
                                                BigDecimal bigP=new BigDecimal(cell.getNumericCellValue());
                                                value = bigP.toString();
                                                //解决1234.0  去掉后面的.0
                                                if(null!=value&&!"".equals(value.trim())){
                                                    String[] item = value.split("[.]");
                                                    if(1<item.length&&"0".equals(item[1])){
                                                        value=item[0];
                                                    }
                                                }
                                                pzPersonWithBLOBs.setpPhone(value);
                                            }else {
                                                pzPersonWithBLOBs.setpPhone(cell.getStringCellValue());
                                            }
                                            break;
                                        case 8:
                                            // 政治面貌
                                            pzPersonWithBLOBs.setpPolitical((sysCodeMapper.getByName("POLITICAL",cell.getStringCellValue().trim())).getCodeNo());
                                            break;
                                        case 9:
                                            // 文化程度
                                            pzPersonWithBLOBs.setpEducation((sysCodeMapper.getByName("DEGREE",cell.getStringCellValue().trim())).getCodeNo());
                                            break;
                                        case 10:
                                            // 婚姻状况
                                            if (cell.getStringCellValue().equals( "未婚")) {
                                                pzPersonWithBLOBs.setpMaritalStatus("01");
                                            } else if (cell.getStringCellValue().equals("已婚") ) {
                                                pzPersonWithBLOBs.setpMaritalStatus("02");
                                            }
                                            break;
                                        case 11:
                                            //宗教信仰
                                            pzPersonWithBLOBs.setpReligion(cell.getStringCellValue());
                                            break;
                                        case 12:
                                            //职业
                                            pzPersonWithBLOBs.setpOccupation(cell.getStringCellValue());
                                            break;
                                        case 13:
                                            //工作单位
                                            pzPersonWithBLOBs.setpWorkUnit(cell.getStringCellValue());
                                            break;
                                        case 14:
                                            //户籍地址
                                            pzPersonWithBLOBs.setpDomicile(cell.getStringCellValue());
                                            break;
                                        case 15:
                                            //籍贯
                                            pzPersonWithBLOBs.setpOrigin(cell.getStringCellValue());
                                            break;
                                        case 16:
                                            //户籍地详址
                                            pzPersonWithBLOBs.setpDetailedDomicile(cell.getStringCellValue());
                                            break;
                                        case 17:
                                            //名下机动车辆
                                            if (cell.getStringCellValue() .equals("有") ) {
                                                pzPersonWithBLOBs.setpVehicles("0");
                                            } else if (cell.getStringCellValue() .equals("无") ) {
                                                pzPersonWithBLOBs.setpVehicles("1");
                                            }
                                            break;
                                        case 18:
                                            //备注
                                            pzPersonWithBLOBs.setpRemarks(cell.getStringCellValue());
                                            break;
                                        case 19:
                                            //有无固定住所
                                            if (cell.getStringCellValue().equals("无") ) {
                                                pzPersonWithBLOBs.setpIsHouse("0");
                                            } else if (cell.getStringCellValue().equals("有") ) {
                                                pzPersonWithBLOBs.setpIsHouse("1");
                                            }
                                            break;
                                        case 20:
                                            //现居地址
                                            pzPersonWithBLOBs.setpAddress(cell.getStringCellValue());
                                            break;
                                        case 21:
                                            //实口类型
                                            if (cell.getStringCellValue().equals("流动人口") ) {
                                                pzPersonWithBLOBs.setFlag1("2");
                                            } else if (cell.getStringCellValue().equals("户籍人口") ) {
                                                pzPersonWithBLOBs.setFlag1("1");
                                            }
                                            break;
                                        case 22:
                                            //车牌号
                                            cell.setCellType(CellType.STRING);
                                            pzPersonWithBLOBs.setStr1(cell.getStringCellValue());
                                            break;
                                        case 23:
                                            // 关注程度
                                            pzKeyYouthWithBLOBs.setAttention((sysCodeMapper.getByName("RENT_CONCERN",cell.getStringCellValue().trim())).getCodeNo());
                                            break;
                                        case 24:
                                            // 管理手段
                                            pzKeyYouthWithBLOBs.setManagementTool((sysCodeMapper.getByName("MANAGEMENT_TOOL",cell.getStringCellValue().trim())).getCodeNo());
                                            break;
                                        case 25:
                                            // 违法情况
                                            pzKeyYouthWithBLOBs.setLllegalSituation((sysCodeMapper.getByName("LLLEGAL_SITUATION",cell.getStringCellValue().trim())).getCodeNo());
                                            break;
                                        case 26:
                                            // 犯罪类型
                                            pzKeyYouthWithBLOBs.setCriminalType((sysCodeMapper.getByName("P_CRIME_TYPE",cell.getStringCellValue().trim())).getCodeNo());
                                            break;
                                        case 27:
                                            // 学校所属层级
                                            pzKeyYouthWithBLOBs.setSchoolHierarchy((sysCodeMapper.getByName("SCHOOL_HIERARCHY",cell.getStringCellValue().trim())).getCodeNo());
                                            break;
                                        case 28:
                                            // 教育学校名称
                                            pzKeyYouthWithBLOBs.setSchoolName(cell.getStringCellValue());
                                            break;
                                        case 29:
                                            //人员类型
                                            //pzKeyYouthWithBLOBs.setPersonTyle(sysCodeMapper.getCodeNoByCodeName(cell.getStringCellValue()));
                                            pzKeyYouthWithBLOBs.setAttention((sysCodeMapper.getByName("PERSON_TYLE",cell.getStringCellValue().trim())).getCodeNo());
                                            break;
                                        default:
                                            toJson.setFlag(0);
                                            toJson.setMsg("err");
                                            break;
                                    }
                                }

                            }
                            List<String> nullArray = new ArrayList();
                            if (pzPersonWithBLOBs.getpGrid() == null) {
                                nullArray.add("所属网格");
                            }
                            if (pzPersonWithBLOBs.getpName() == null) {
                                nullArray.add("姓名");
                            }
                            if (pzPersonWithBLOBs.getpSex() == null) {
                                nullArray.add("性别");
                            }
                            if (pzPersonWithBLOBs.getpCradId() == null) {
                                nullArray.add("身份证号");
                            }
                            if (pzPersonWithBLOBs.getFlag1() == null) {
                                nullArray.add("实口类型");
                            }
                            if (pzPersonWithBLOBs.getpOrigin() == null) {
                                nullArray.add("籍贯");
                            }
                            if (pzPersonWithBLOBs.getpIsHouse() == null) {
                                nullArray.add("有无固定住所");
                            }
                            if (pzPersonWithBLOBs.getpAddress() == null) {
                                nullArray.add("现居地址");
                            }
                            if (pzPersonWithBLOBs.getpMaritalStatus() == null) {
                                nullArray.add("婚姻状况");
                            }
                            if (pzPersonWithBLOBs.getpDomicile() == null) {
                                nullArray.add("户籍地址");
                            }
                            if (pzPersonWithBLOBs.getpDetailedDomicile() == null) {
                                nullArray.add("户籍地详情");
                            }
                            if (pzPersonWithBLOBs.getpVehicles() == null) {
                                nullArray.add("名下机动车辆");
                            }
                            if (pzKeyYouthWithBLOBs.getManagementTool() == null) {
                                nullArray.add("管理手段");
                            }
                            if (pzKeyYouthWithBLOBs.getLllegalSituation() == null) {
                                nullArray.add("违法情况");
                            }
                            if (pzKeyYouthWithBLOBs.getCriminalType() == null) {
                                nullArray.add("犯罪类型");
                            }
                            if (pzKeyYouthWithBLOBs.getPersonTyle() == null) {
                                nullArray.add("人员类型");
                            }
                            if (nullArray.size() > 0) {
                                toJson.setObject(nullArray);
                                toJson.setMsg("必填字段不能为空，请重新导入");
                            } else {

                                //判断用户是否为空，不为空判断用户是否存在
                                if (!StringUtils.checkNull(pzPersonWithBLOBs.getpName()) && !StringUtils.checkNull(pzPersonWithBLOBs.getpCradId())) {
                                    Map<String, Object> map = new HashMap<String, Object>();
                                    map.put("pCradId", pzPersonWithBLOBs.getpCradId());
                                    map.put("pName", pzPersonWithBLOBs.getpName());
                                    PzPersonWithBLOBs pzPersons = pzPersonMapper.selectPersonCountByNameCrad(map);
                                    if (pzPersons.getCount() > 0) {
                                        pzPersonWithBLOBs.setpId(pzPersons.getpId());
                                        pzPersonWithBLOBs.setFlag2("0");
                                        if(!pzPersons.getSpecialStr().contains("6,")){
                                            String str = "6," + pzPersons.getSpecialStr();
                                            pzPersonWithBLOBs.setSpecialStr(str);
                                        }
                                        pzKeyYouthWithBLOBs.setUserId(String.valueOf(pzPersons.getpId()));
                                        pzPersonMapper.updateByPrimaryKeySelective(pzPersonWithBLOBs);
                                    } else {
                                        //创建用户和创建时间
                                        pzPersonWithBLOBs.setCreateUser(users.getUserId());
                                        pzPersonWithBLOBs.setCreateTime(DateFormat.getCurrentTime());
                                        pzPersonWithBLOBs.setFlag2("0");
                                        pzPersonWithBLOBs.setSpecialStr("6,");
                                        pzPersonMapper.insertSelective(pzPersonWithBLOBs);
                                        pzKeyYouthWithBLOBs.setUserId(String.valueOf(pzPersonWithBLOBs.getpId()));
                                    }
                                }
                                //将数据放入数据库
                                //用户表与肇事表关联用户userid
                                pzKeyYouthWithBLOBs.setUserId(String.valueOf(pzPersonWithBLOBs.getpId()));
                                //判断关联用户userid是否为空,为空新增,不为空修改
                                if (!StringUtils.checkNull(pzKeyYouthWithBLOBs.getUserId())) {
                                    //如果不为空，查询是否有肇事信息，有则更新，否则新增
                                    Map<String, Object> map = new HashMap<String, Object>();
                                    map.put("userId", pzKeyYouthWithBLOBs.getUserId());
                                    PzKeyYouth pzKeyY = pzKeyYouthMapper.selectByUserId(map);
                                    if (pzKeyY.getCou() > 0) {
                                        pzKeyYouthWithBLOBs.setKeyYouthId(pzKeyY.getKeyYouthId());
                                        count +=  pzKeyYouthMapper.updateByPrimaryKeySelective(pzKeyYouthWithBLOBs);
                                    } else {
                                        //创建用户和创建时间
                                        pzKeyYouthWithBLOBs.setCreateUser(users.getUserId());
                                        pzKeyYouthWithBLOBs.setCreateTime(DateFormat.getCurrentTime());
                                        count += pzKeyYouthMapper.insertSelective(pzKeyYouthWithBLOBs);
                                    }
                                }
                        }


                        }
                    }
                    if (count > 0) {
                        successCount = successCount + count;
                    }
                    dest.delete();
                    if (successCount > 0) {
                        toJson.setTotleNum(successCount);
                        toJson.setMsg("ok");
                        toJson.setFlag(0);
                        return toJson;
                    }
                }
            }
        } catch (Exception e) {
            //读取配置文件,获取文件要存储的路径
            StringBuffer path = this.getPath();
            String fileName = file.getOriginalFilename();
            if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                String extName = fileName
                        .substring(fileName.lastIndexOf(".") + 1);// 文件后缀
                String newFileName = uuid + extName;//组合成新的文件名
                File dest = new File(path.toString(), newFileName);
                dest.delete();
            }
            e.getMessage();
            e.printStackTrace();
        }

        return toJson;
    }

    //判读当前系统,读取配置文件,返回路径
    public StringBuffer getPath(){
        ResourceBundle rb = ResourceBundle.getBundle("upload");
        String osName = System.getProperty("os.name");
        StringBuffer path = new StringBuffer();
        if (osName.toLowerCase().startsWith("win")) {
            path = path.append(rb.getString("upload.win"));
        } else {
            path = path.append(rb.getString("upload.linux"));
        }
        return path;
    }
}
